package com;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import org.apache.ibatis.jdbc.ScriptRunner;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

/**
 * @ClassName: MyBatisPlusGenerator
 * @Description:
 * @Author: lu.sy
 * @Date: 2021/11/22
 */
public class MyBatisPlusGenerator {
    /**
     * 数据库URL
     */
    private static final String jdbcUrl ="jdbc:mysql://localhost:3308/mall_tiny?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai";
    /**
     * 数据库用户名
     */
    private static final String userName = "root";
    /**
     * 数据库密码
     */
    private static final String password = "123456";
    /**
     * 作者
     */
    private static final String author = "lu.sy";
    /**
     * 父包模块名，没有配置""
     */
    private static final String parentModuleName = "sm-dao";
    /**
     * 父包名
     */
    private static final String moduleParentPath = "com.sy";
    /**
     * 模块名，没有配置""
     */
    private static final String moduleName = "order";
    /**
     * 表前缀，没有配置""，多个用逗号分隔，如：t_,sys_
     */
    private static final String tablePrefix = "ums_";
    /**
     * 生成的表名称，多个用逗号分隔，如：t_,sys_
     */
    private static final String includeTable = "";

    /**
     * 数据源配置
     */
    private static DataSourceConfig dataSourceConfig(){
        return new DataSourceConfig.Builder(jdbcUrl,userName,password)
                 .dbQuery(new MySqlQuery())
                 .schema("mybatis-plus")
                 .typeConvert(new MySqlTypeConvert())
                 .build();
    }
    /**
     * 全局配置
     */
    private static GlobalConfig globalConfig() {
        String outputDir = System.getProperty("user.dir");
        if (StrUtil.isNotBlank(parentModuleName)){
            outputDir +="/"+parentModuleName;
        }
        outputDir +="/src/main/java";
        return new GlobalConfig.Builder()
                .fileOverride()
                .outputDir(outputDir)
                .disableOpenDir() //禁用打开文件夹
                .author(author)
                .enableSwagger()
                .dateType(DateType.TIME_PACK)
                .commentDate("yyyy-MM-dd")
                .build();
    }

    /**
     * 包配置
     */
    private static PackageConfig packageConfig() {
        return new PackageConfig.Builder()
                //父包名，默认值:com.baomidou
                .parent(moduleParentPath)
                //父包模块名，默认值:无
                .moduleName(moduleName)
//                .entity("entity")
//                .service("service")
//                .serviceImpl("service.impl")
//                .mapper("mapper")
//                .xml("mapper.xml")
                .build();
    }
    /**
     * 策略配置
     */
    private static StrategyConfig strategyConfig() {
        StrategyConfig.Builder strategy = new StrategyConfig.Builder();
        if (StrUtil.isNotBlank(tablePrefix)){
            strategy.addTablePrefix(tablePrefix);
        }
        if (StrUtil.isNotBlank(includeTable)){
            strategy.addInclude(includeTable);
        }
        strategy.addExclude()
                .entityBuilder()
                //禁用生成 serialVersionUID 默认值:生成 serialVersionUID
//                .disableSerialVersionUID()
                .enableChainModel()
                .enableLombok()
                .enableRemoveIsPrefix()
                .enableTableFieldAnnotation()
                .naming(NamingStrategy.underline_to_camel)
                .columnNaming(NamingStrategy.underline_to_camel)
                .addTableFills(new Column("create_time", FieldFill.INSERT))
                .addTableFills(new Property("update_time", FieldFill.INSERT_UPDATE))
                .formatFileName("%sEntity")
                .controllerBuilder()
                .enableRestStyle()
                .mapperBuilder()
                .enableBaseResultMap()
                .enableBaseColumnList()
                ;
        return strategy.build();
    }
    /**
     * 模板配置
     */
    private static TemplateConfig.Builder templateConfig() {
        return new TemplateConfig.Builder()
                //禁用controller
                .disable(TemplateType.CONTROLLER)
                ;
    }

    public static void main(String[] args) {
        AutoGenerator generator = new AutoGenerator(dataSourceConfig());
        generator.global(globalConfig());
        generator.packageInfo(packageConfig());
        generator.strategy(strategyConfig());
        generator.template(
                templateConfig()
                //自定义entity模板
                .entity("/templates/entity.java.vm")
                .build()
        );
        generator.execute();
    }

}
